﻿#include "UserInfo.h"
#include "DatabaseMgmt.h"


CUserInfo::CUserInfo()
{
	init();
}

CUserInfo::~CUserInfo()
{

}

void CUserInfo::init()
{
}


uint32_t CUserInfo::queryUserInfo(uint32_t uid, UserInfo& info)
{
	int ret = 0;


	return ret;
}

uint32_t CUserInfo::queryUserInfo(QString strAccountName, UserInfo& info)
{
	int ret = 0;

	QString strSql = QString("SELECT UID, ACCOUNT_NAME, ACCOUNT_PASSWD,"
		"USER_NAME, USER_PHONE, ALLOW_DELETE FROM TB_USER_INFO WHERE ACCOUNT_NAME='%1'")
		.arg(strAccountName);
	QSqlQuery query;

	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	while (query.next())
	{
		info.uid = query.value("UID").toUInt();
		info.account_name = query.value("ACCOUNT_NAME").toString();
		info.account_password = query.value("ACCOUNT_PASSWD").toString();
		info.user_name = query.value("USER_NAME").toString();
		info.user_phone = query.value("USER_PHONE").toString();
		info.allow_delete = query.value("ALLOW_DELETE").toBool();
	}

	return ret;
}

uint32_t CUserInfo::checkeUserInfo(QString strAccountName, QString strAccountPassword)
{
	int ret = 0;
	QCryptographicHash md5(QCryptographicHash::Md5);

	QString strMd5 = strAccountName + "@" + strAccountPassword;
	md5.addData(strMd5.toLocal8Bit());

	QString strPassword = md5.result().toHex();
	UserInfo user_info;
	queryUserInfo(strAccountName, user_info);
	if (user_info.uid == 0)
	{
		ret = CONST_ERR_NON_EXISTENT;
	}
	else
	{
		if (user_info.account_password == strPassword)
		{
			ret = CONST_ERR_SUCCESS;
		}
		else
		{
			ret = CONST_ERR_PASSWORD_INVALID;
		}
	}
	return ret;
}

uint32_t CUserInfo::removeUserInfo(uint32_t uid)
{
	int ret = 0;


	return ret;
}

uint32_t CUserInfo::removeUserInfo(QString strAccountName)
{
	int ret = 0;


	return ret;
}


